home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2013 December / PCGO_13-12_CD.iso / nw.pak / Unnamed File 000845.unknown < prev    next >
Encoding:
Text File  |  2012-12-14  |  8.5 KB  |  312 lines

  1. function Window(routing_id) {
  2.   // Get and set id.
  3.   var id = global.__nwObjectsRegistry.allocateId();
  4.   Object.defineProperty(this, 'id', {
  5.     value: id,
  6.     writable: false
  7.   });
  8.  
  9.   // Store routing id (need for IPC since we are in node's context).
  10.   this.routing_id = routing_id;
  11.  
  12.   // Store myself in node's context.
  13.   global.__nwWindowsStore[id] = this;
  14.   global.__nwObjectsRegistry.set(id, this);
  15.  
  16.   // Tell Shell I'm the js delegate of it.
  17.   native function BindToShell();
  18.   BindToShell(this.routing_id, this.id);
  19. }
  20.  
  21. // Window will inherit EventEmitter in "third_party/node/src/node.js", do
  22. // not inherit here becuase this file is loaded before everything else.
  23.  
  24. // And init everything after the inheritance.
  25. Window.init = function() {
  26.  
  27. var v8_util = process.binding('v8_util');
  28. var EventEmitter = process.EventEmitter;
  29.  
  30. native function CallObjectMethod();
  31. native function CallObjectMethodSync();
  32.  
  33. // Override the addListener method.
  34. Window.prototype.on = Window.prototype.addListener = function(ev, callback) {
  35.   // Save window id of where the callback is created.
  36.   var closure = v8_util.getCreationContext(callback);
  37.   if (v8_util.getConstructorName(closure) == 'Window' && 
  38.       closure.hasOwnProperty('nwDispatcher')) {
  39.     v8_util.setHiddenValue(callback, '__nwWindowId',
  40.         closure.nwDispatcher.requireNwGui().Window.get().id);
  41.   }
  42.  
  43.   // Call parent.
  44.   EventEmitter.prototype.addListener.apply(this, arguments);
  45. }
  46.  
  47. // Route events.
  48. Window.prototype.handleEvent = function(ev) {
  49.   // Filter invalid callbacks.
  50.   var listeners_copy = this.listeners(ev).slice(0);
  51.   for (var i = 0; i < listeners_copy.length; ++i) {
  52.     var original_closure = v8_util.getCreationContext(listeners_copy[i]);
  53.  
  54.     // Skip for node context. 
  55.     if (v8_util.getConstructorName(original_closure) != 'Window')
  56.       continue;
  57.  
  58.     var window_id = v8_util.getHiddenValue(listeners_copy[i], '__nwWindowId');
  59.  
  60.     // Remove callback if original window is closed (not in __nwWindowsStore).
  61.     if (global.__nwWindowsStore.hasOwnProperty(window_id)) {
  62.       // Check hashes and see if the window context of Shell has been changed,
  63.       // this happens when we refresh the Shell or change it's locations.
  64.       var original_hash = v8_util.getObjectHash(original_closure);
  65.       var current_hash = v8_util.getObjectHash(
  66.           global.__nwWindowsStore[window_id].window);
  67.  
  68.       // Do nothing if nothing is changed.
  69.       if (original_hash == current_hash)
  70.         continue;
  71.     }
  72.  
  73.     console.warn('Remove zombie callback for window id ' + window_id);
  74.     this.removeListener(ev, listeners_copy[i]);
  75.   }
  76.  
  77.   // Route events to EventEmitter.
  78.   this.emit.apply(this, arguments);
  79.  
  80.   // If no one is listening to 'close' then close directly
  81.   if (ev == 'close' && this.listeners(ev).length == 0) {
  82.     this.close(true);
  83.     return;
  84.   } else if (ev == 'closed') { // Clear me.
  85.     delete global.__nwWindowsStore[this.id];
  86.     return;
  87.   }
  88. }
  89.  
  90. // Return current window object of Shell's DOM.
  91. Window.prototype.__defineGetter__('window', function() {
  92.   native function GetWindowObject();
  93.   return GetWindowObject(this.routing_id);
  94. });
  95.  
  96. Window.prototype.__defineSetter__('x', function(x) {
  97.   this.moveTo(x, this.y);
  98. });
  99.  
  100. Window.prototype.__defineGetter__('x', function() {
  101.   return CallObjectMethodSync(this, 'GetPosition', [])[0];
  102. });
  103.  
  104. Window.prototype.__defineSetter__('y', function(y) {
  105.   this.moveTo(this.x, y);
  106. });
  107.  
  108. Window.prototype.__defineGetter__('y', function() {
  109.   return CallObjectMethodSync(this, 'GetPosition', [])[1];
  110. });
  111.  
  112. Window.prototype.__defineSetter__('width', function(width) {
  113.   this.resizeTo(width, this.height);
  114. });
  115.  
  116. Window.prototype.__defineGetter__('width', function() {
  117.   return CallObjectMethodSync(this, 'GetSize', [])[0];
  118. });
  119.  
  120. Window.prototype.__defineSetter__('height', function(height) {
  121.   this.resizeTo(this.width, height);
  122. });
  123.  
  124. Window.prototype.__defineGetter__('height', function() {
  125.   return CallObjectMethodSync(this, 'GetSize', [])[1];
  126. });
  127.  
  128. Window.prototype.__defineSetter__('title', function(title) {
  129.   this.window.document.title = title;
  130. });
  131.  
  132. Window.prototype.__defineGetter__('title', function() {
  133.   return this.window.document.title;
  134. });
  135.  
  136. Window.prototype.__defineSetter__('menu', function(menu) {
  137.   if (v8_util.getConstructorName(menu) != 'Menu')
  138.     throw new String("'menu' property requries a valid Menu");
  139.  
  140.   if (menu.type != 'menubar')
  141.     throw new String('Only menu of type "menubar" can be used as this.window menu');
  142.  
  143.   v8_util.setHiddenValue(this, 'menu', menu);
  144.   CallObjectMethod(this, 'SetMenu', [ menu.id ]);
  145. });
  146.  
  147. Window.prototype.__defineGetter__('menu', function() {
  148.   return v8_util.getHiddenValue(this, 'menu');
  149. });
  150.  
  151. Window.prototype.__defineSetter__('isFullscreen', function(flag) {
  152.   if (flag)
  153.     this.enterFullscreen();
  154.   else
  155.     this.leaveFullscreen();
  156. });
  157.  
  158. Window.prototype.__defineGetter__('isFullscreen', function() {
  159.   var result = CallObjectMethodSync(this, 'IsFullscreen', []);
  160.   return Boolean(result[0]);
  161. });
  162.  
  163. Window.prototype.__defineSetter__('isKioskMode', function(flag) {
  164.   if (flag)
  165.     this.enterKioskMode();
  166.   else
  167.     this.leaveKioskMode();
  168. });
  169.  
  170. Window.prototype.__defineGetter__('isKioskMode', function() {
  171.   var result = CallObjectMethodSync(this, 'IsKioskMode', []);
  172.   return Boolean(result[0]);
  173. });
  174.  
  175. Window.prototype.moveTo = function(x, y) {
  176.   CallObjectMethod(this, 'MoveTo', [ Number(x), Number(y) ]);
  177. }
  178.  
  179. Window.prototype.moveBy = function(x, y) {
  180.   var position = CallObjectMethodSync(this, 'GetPosition', []);
  181.   this.moveTo(position[0] + x, position[1] + y);
  182. }
  183.  
  184. Window.prototype.resizeTo = function(width, height) {
  185.   CallObjectMethod(this, 'ResizeTo', [ Number(width), Number(height) ]);
  186. }
  187.  
  188. Window.prototype.resizeBy = function(width, height) {
  189.   var size = CallObjectMethodSync(this, 'GetSize', []);
  190.   this.resizeTo(size[0] + width, size[1] + height);
  191. }
  192.  
  193. Window.prototype.focus = function(flag) {
  194.   if (typeof flag == 'undefined' || Boolean(flag))
  195.     this.window.focus();
  196.   else
  197.     this.blur();
  198. }
  199.  
  200. Window.prototype.blur = function() {
  201.   this.window.blur();
  202. }
  203.  
  204. Window.prototype.show = function(flag) {
  205.   if (typeof flag == 'undefined' || Boolean(flag))
  206.     CallObjectMethod(this, 'Show', []);
  207.   else
  208.     this.hide();
  209. }
  210.  
  211. Window.prototype.hide = function() {
  212.   CallObjectMethod(this, 'Hide', []);
  213. }
  214.  
  215. Window.prototype.hide = function() {
  216.   CallObjectMethod(this, 'Hide', []);
  217. }
  218.  
  219. Window.prototype.close = function(force) {
  220.   CallObjectMethod(this, 'Close', [ Boolean(force) ]);
  221. }
  222.  
  223. Window.prototype.maximize = function() {
  224.   CallObjectMethod(this, 'Maximize', []);
  225. }
  226.  
  227. Window.prototype.unmaximize = function() {
  228.   CallObjectMethod(this, 'Unmaximize', []);
  229. }
  230.  
  231. Window.prototype.minimize = function() {
  232.   CallObjectMethod(this, 'Minimize', []);
  233. }
  234.  
  235. Window.prototype.restore = function() {
  236.   CallObjectMethod(this, 'Restore', []);
  237. }
  238.  
  239. Window.prototype.enterFullscreen = function() {
  240.   CallObjectMethod(this, 'EnterFullscreen', []);
  241. }
  242.  
  243. Window.prototype.leaveFullscreen = function() {
  244.   CallObjectMethod(this, 'LeaveFullscreen', []);
  245. }
  246.  
  247. Window.prototype.toggleFullscreen = function() {
  248.   CallObjectMethod(this, 'ToggleFullscreen', []);
  249. }
  250.  
  251. Window.prototype.enterKioskMode = function() {
  252.   CallObjectMethod(this, 'EnterKioskMode', []);
  253. }
  254.  
  255. Window.prototype.leaveKioskMode = function() {
  256.   CallObjectMethod(this, 'LeaveKioskMode', []);
  257. }
  258.  
  259. Window.prototype.toggleKioskMode = function() {
  260.   CallObjectMethod(this, 'ToggleKioskMode', []);
  261. }
  262.  
  263. Window.prototype.showDevTools = function() {
  264.   CallObjectMethod(this, 'ShowDevTools', []);
  265. }
  266.  
  267. Window.prototype.setMinimumSize = function(width, height) {
  268.   CallObjectMethod(this, 'SetMinimumSize', [ width, height ]);
  269. }
  270.  
  271. Window.prototype.setMaximumSize = function(width, height) {
  272.   CallObjectMethod(this, 'SetMaximumSize', [ width, height ]);
  273. }
  274.  
  275. Window.prototype.setResizable = function(resizable) {
  276.   resizable = Boolean(resizable);
  277.   CallObjectMethod(this, 'SetResizable', [ resizable ]);
  278. }
  279.  
  280. Window.prototype.setAlwaysOnTop = function(flag) {
  281.   CallObjectMethod(this, 'SetAlwaysOnTop', [ Boolean(flag) ]);
  282. }
  283.  
  284. Window.prototype.requestAttention = function(flash) {
  285.   flash = Boolean(flash);
  286.   CallObjectMethod(this, 'RequestAttention', [ flash ]);
  287. }
  288.  
  289. Window.prototype.setPosition = function(position) {
  290.   if (position != 'center' && position != 'mouse')
  291.     throw new String('Invalid postion');
  292.   CallObjectMethod(this, 'SetPosition', [ position ]);
  293. }
  294.  
  295. Window.prototype.reload = function(type) {
  296.   // type is default to 0 (RELOAD).
  297.   if (!(typeof type == 'number' && 0 <= type && type <= 2))
  298.     type = 0;
  299.  
  300.   CallObjectMethod(this, 'Reload', [ type ]);
  301. }
  302.  
  303. Window.prototype.reloadIgnoringCache = function() {
  304.   this.reload(1);
  305. }
  306.  
  307. Window.prototype.reloadOriginalRequestURL = function() {
  308.   this.reload(2);
  309. }
  310.  
  311. }  // function Window.init
  312.